package com.ozero1st;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class type3 extends Activity{
	private static final int DIALOG_DOWNLOAD_PROGRESS = 0;
    private ProgressDialog mProgressDialog;
    Button btnDown;
	    @Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.typ3);
		
		btnDown = (Button)findViewById(R.id.btnDown);
		btnDown.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				// TODO Auto-generated method stub
				startDownload("http://img124.imageshack.us/img124/5500/24983803ky7.gif","test.gif", "N");
			}
			
		});
	}

		

		private void startDownload(String fileurl , String filesavename ,String installapk) {

	        new DownloadFileAsync().execute(fileurl , filesavename , installapk);

	      }
		

		@Override
		protected Dialog onCreateDialog(int id) {

		          ProgressDialog mProgressDialog;
				switch (id) {

		  case DIALOG_DOWNLOAD_PROGRESS:

		  mProgressDialog = new ProgressDialog(this);

		  mProgressDialog.setMessage("Downloading file..");

		  mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

		  mProgressDialog.setCancelable(false);

		  mProgressDialog.show();

		  return mProgressDialog;

		  default:

		  return null;

		          }

		      }

	    class DownloadFileAsync extends AsyncTask<String, String, String> {

		     

	  	  @Override

	  	  protected void onPreExecute() {

	  	  super.onPreExecute();

	  	  showDialog(DIALOG_DOWNLOAD_PROGRESS);

	  	  }


	  	  @Override

	  	  protected String doInBackground(String... aurl) {

	  	  int count;


	  	  try {


	  	  URL url = new URL(aurl[0]);

	  	  URLConnection conexion = url.openConnection();

	  	  conexion.connect();

	  	 


	  	String filesavename = aurl[1];

	  	int lenghtOfFile = conexion.getContentLength();

	  	String path = Environment.getExternalStorageDirectory().toString();

	  	     

	  	String installapk = aurl[2];

	  	 

	  	            // download the file

	  	            BufferedInputStream input = new BufferedInputStream(url.openStream());

	  	            FileOutputStream output = new FileOutputStream(path + "/" + filesavename);


	  	            byte data[] = new byte[1024];


	  	        long total = 0;

	  	       

	  	            while ((count = input.read(data)) != -1) {

	  	           

	  	            total += count;

	  	  publishProgress(""+(int)((total*100)/lenghtOfFile));

	  	 

	  	 

	  	                output.write(data, 0, count);

	  	            }


	  	            output.flush();

	  	            output.close();

	  	            input.close();

	  	     

	  	            if (installapk.equals("Y"))

	  	            {

	  	            //INSTALL APK AFTER COMPLETE

	  	            Intent intent = new Intent(Intent.ACTION_VIEW);

			  	    intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "venus.apk" )), "application/vnd.android.package-archive");
		
			  	    startActivity(intent);  

	  	            }

	  	  }

	  	  catch (Exception e)

	  	  {
	  		  
	  	  String err = e.getMessage();
	  	  Log.i(this.toString(), "error : " +err); 
	  	  
	  	  }

	  	  return null;


	  	  }

	  	  protected void onProgressUpdate(String... progress) {

	  	  // Log.d("ANDRO_ASYNC",progress[0]);

	  	  mProgressDialog.setProgress(Integer.parseInt(progress[0]));

	  	  }


	  	  @Override

	  	  protected void onPostExecute(String unused) {

	  	  dismissDialog(DIALOG_DOWNLOAD_PROGRESS);

	  	  }

	  	  }
   
	}
